<?php

require_once 'library/amazon.class.php';
$arModes = array (
    'baby' => 'baby (Baby)',
    'books' => 'books (Books)',
    'classical' => 'classical (Classical Music)',
    'dvd' => 'dvd (DVD)',
    'electronics' => 'electronics (Electronics)',
    'garden' => 'garden (Outdoor Living)',
    'kitchen' => 'kitchen (Kitchen & Housewares)',
    'magazines' => 'magazines (Magazines)',
    'music' => 'music (Popular Music)',
    'pc-hardware' => 'pc-hardware (Computers)',
    'photo' => 'photo (Camera & Photo)',
    'software' => 'software (Software)',
    'toys' => 'toys (Toys & Games)',
    'universal' => 'universal (Tools & Hardware)',
    'vhs' => 'vhs (Video)',
    'videogames' => 'videogames (Computer & Video Games)'
);


// Sort Types
$arModeSortType = array (
    'baby' => array(
        '+pmrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical (A-Z)',
    ),
    'books' => array(
        '+pmrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+reviewrank' => 'Average Customer Review',
        '+pricerank' => 'Price (Low to High)',
        '+inverse-pricerank' => 'Price (High to Low)',
        '+daterank' => 'Publication Date',
        '+titlerank' => 'Alphabetical (A-Z)',
        '-titlerank' => 'Alphabetical (Z-A)',
    ),
    'classical' => array(
        '+pmrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical (A-Z)',
    ),
    'dvd' => array(
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical',
    ),
    'electronics' => array(
        '+pmrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical',
        '+reviewrank' => 'Review',
    ),
    'garden' => array(
        '+psrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical (A-Z)',
        '-titlerank' => 'Alphabetical (Z-A)',
        '+manufactrank' => 'Manufacturer (A-Z)',
        '-manufactrank' => 'Manufacturer (Z-A)',
        '+price' => 'Price (Low to High)',
        '-price' => 'Price (High to Low)',
    ),
    'kitchen' => array(
        '+psrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical (A-Z)',
        '-titlerank' => 'Alphabetical (Z-A)',
        '+manufactrank' => 'Manufacturer (A-Z)',
        '-manufactrank' => 'Manufacturer (Z-A)',
        '+price' => 'Price (Low to High)',
        '-price' => 'Price (High to Low)',
    ),
    'magazines' => array(
        '+pmrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical (A-Z)',
    ),
    'music' => array(
        '+psrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+artistrank' => 'Artist Name',
        '+orig-rel-date' => 'Original Release Date',
        '+titlerank' => 'Alphabetical',
    ),
    'pc-hardware' => array(
        '+psrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical (A-Z)',
        '-titlerank' => 'Alphabetical (Z-A)',
    ),
    'photo' => array(
        '+pmrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical (A-Z)',
        '-titlerank' => 'Alphabetical (Z-A)',
    ),
    'software' => array(
        '+pmrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical',
        '+price' => 'Price (Low to High)',
        '+price' => 'Price (High to Low)',
    ),
    'toys' => array(
        '+pmrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical (A-Z)',
    ),
    'universal' => array(
        '+psrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical (A-Z)',
        '-titlerank' => 'Alphabetical (Z-A)',
        '+manufactrank' => 'Manufacturer (A-Z)',
        '-manufactrank' => 'Manufacturer (Z-A)',
        '+price' => 'Price (Low to High)',
        '-price' => 'Price (High to Low)',
    ),
    'vhs' => array(
        '+psrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical',
    ),
    'videogames' => array(
        '+pmrank' => 'Featured Items',
        '+salesrank' => 'Bestselling',
        '+titlerank' => 'Alphabetical',
        '+price' => 'Price (Low to High)',
        '-price' => 'Price (High to Low)',
    ),
);

$keys = $_GET['search'];

?>

<style>
.elements {
	padding:10px;
}

.item {
	padding:10px;
}

.title {
	font-size:120%;
}

.content {
	font-size:100%;
}

.snippet {
	font-size:80%;
}
</style>

<?php
if (isset($keys)) {	
	
	$items = amazon_search_simple_search($keys);
	foreach ($items as $item) {		
	  $products[] = array(
	    'title' => $item['title'],
	    'link' => $item['detailpageurl'],
	    'type' => $item['productgroup'],
	    'asin' => $item['asin'],
	    'isbn' => $item['isbn'],
	    'user' => isset($item['participants']) ? implode(', ', $item['participants']) : '',
	    'snippet' => isset($item['editorialreviews']) ? $item['editorialreviews'][0]['content'] : '',
	  );
	}
	
	foreach ($products as $product) {
	  
		echo '<div class="elements">';
			echo '<div class="item">';
				echo '<div class="title">';
				echo '<a href="'.$product['link'].'">'.$product['title'].'</a>';
				echo '</div>';
				
				if (!empty($product['isbn'])) {
					echo '<div class="content">';
					echo 'ISBN: '.$product['isbn'];
					echo '</div>';
				}				
				echo '<div class="snippet">';
				echo $product['snippet'];
				echo '</div>';			
			echo '</div>';		
		echo '</div>';
	 
	  
	}
	//var_dump($products);
}
/* <?php echo $_GET['search']; ?> */ 
?>

<h1>Anacondha Sample page</h1>
<br />
<form action="sample.php" method="GET">
Search for <input type="text" name="search" value="Fiona Apple">

<select name="Mode">
<?php 
	foreach ($arModes as $sMode => $sDisplay) {
		echo "\n". '	<option value="'. $sMode .'"';
		if ($sCurrentMode == $sMode) {
			echo ' selected';
		}
		echo '>'. $sDisplay .'</option>';
	}
?>
</select>
 sorted by 
<select name="SortBy">
<?php 
	foreach ($arModeSortType[$sCurrentMode] as $sModeSortType => $sDisplay) {
		echo "\n". '	<option value="'. $sModeSortType .'"';
		if ($sCurrentModeSortType == $sModeSortType) {
			echo ' selected';
		}
		echo '>'. $sDisplay .'</option>';
	}
?>
</select>
<br /> 
<input type="submit"/>
</form>